<?php
// include function files for this application
require_once('/home/domains/uny-con.com/public_html/uny-con_fns.php');
if (!session_id()) { session_start(); }

   do_html_header("UNY-Con Create text file of events", staysmall, $uny);

    //connect to the DB
   if (!($conn = db_connect()))
     return false;

$prev_event_array[] = "";

$filename = 'allevents.txt';
//fopen ($filename, 'w+');
if (is_writable($filename)) {
	// open file as writeable and zero it.
	if (!$fp = fopen($filename, 'w+b')) {
		print "<span class=\"redtext\">Cannot open file ($filename)</span>";
		exit;
		}
	//print "Success, wrote ($someconent entries) to file ($filename)";
	fclose($fp);
	} # close iswriteable if
	else {
	echo "<span class=\"redtext\">File $filename is not writeable!</span>";
	}

$result = pg_query("SELECT attendee.first_name, attendee.last_name, event_time.start_time, event_time.assigned_room, event.* FROM event_time, event, attendee WHERE event.event_id = event_time.event_id AND event.attendee_id = attendee.attendee_id AND cancelled != 't' AND approved = 't' ORDER BY event.event_type ASC, event_time.start_time ASC, event.game_system ASC, attendee.last_name ASC");
$num = pg_numrows($result);
$i=0;
while ($i < $num) {
	$values = recordproc($result, $i);
	extract($values);
	//This checks to see what timeslot the current record is in. If we have moved to a new timeslot from the previous record we clos the previous table and start a new one, otherwise we write another table cell.
	$current_event_type = $event_type;
	if ($working_event_type != $current_event_type) {
		$working_event_type = $current_event_type;
		$somecontent = "\n$current_event_type !!Heading!!";
		write_to_file($somecontent, $filename);
		}
	$current_ts = $start_time;
	if ($working_ts != $current_ts) {
		$working_ts = $current_ts;
		$tstime = print_timeslot($current_ts);
		$somecontent = "\n$tstime";
		write_to_file($somecontent, $filename);
		}
		unset($processed_numts, $processed_equip, $processed_event_id, $processed_maturity, $print_description);
		//this gets rid of mulitple \n characters.
		$print_description = str_replace("\n\n" , "", $description);
		if ($num_of_timeslots != "1") {$processed_event_id = "TS$start_time"."$event_id*"; }
			else {$processed_event_id = "TS$start_time"."$event_id"; }
		if ($num_of_timeslots != "1") {$processed_numts = "\n*Note, this event lasts $num_of_timeslots timeslots."; }
		if ($equipment != "") {$processed_equip = "\nAttendees should bring: $equipment."; }
		if ($maturity == "M") {$processed_maturity = "\nThis event may contain mature themes."; }
		if ($maturity == "Y") {$processed_maturity = "\nThis event will contain mature themes."; }
		if (array_key_exists($event_id, $prev_event_array)) {
			$print_description = "Please see event description for event TS".$prev_event_array["$event_id"]."$event_id in timeslot ".$prev_event_array["$event_id"]." of $event_type events.";
			unset($processed_equip, $processed_maturity);
			}
		$somecontent = "\n#$processed_event_id
Title: $event_title
Difficulty Level: $difficulty&&&Knowledge Level: $familiarity
Location: $assigned_room
Game System: $game_system
Event Description: $print_description$processed_maturity$processed_numts$processed_cost$processed_equip\nGM: $name";
		write_to_file($somecontent, $filename);
		if (!array_key_exists($event_id, $prev_event_array)) 
			$prev_event_array["$event_id"] = "$start_time";
		unset($somecontent);
	$i++;
	}

do_html_URL("$filename", "Download $filename");
/*
while ($element = each($prev_event_array)) {
	echo $element['key'];
	echo ' - ';
	echo $element['value'];
	echo '<br />';
	}*/
print_brs(24);
do_html_footer(news);